﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EquipViewAppNet;
using SecsEquip;

namespace EquipSecs300
{
    public partial class ProcessView : CMyFormEx
    {
        GEM_Recipe pGEMRecipe;
        // 程式路径
        string recipePath = null;
        bool PPIsFolder;
        bool PPAllowHostDowmload;

        public ProcessView( )
        {
            m_pViewName = "过程处理";

            InitializeComponent();

            listViewProcessProgram.Columns.Add("文件", 826);
        }

        // 初始化
        public override void UpdateData()
        {
            pGEMRecipe = secsEquipHelper.Recipe;

            recipePath = SecsParameterHelper.SecsParam.ProcessProgramPath;
            PPIsFolder = SecsParameterHelper.SecsParam.PPIsFolder;
            PPAllowHostDowmload = SecsParameterHelper.SecsParam.PPAllowHostDowmload;

            RefreshView();
        }
        
        // 选择程式路径
        private void buttonChooseRecipePath_Click(object sender, EventArgs e)
        {
            string pRecipePath = UtilitiesFile.ChooseFilePath();
            if (pRecipePath != null)
            {
                recipePath = pRecipePath;
                textBoxRecipePath.Text = recipePath;

                ResfeshRecipeList(recipePath, PPIsFolder);       // 刷新
                m_pSecs.SetRecipeDirectory(pRecipePath);      // 设置

                SecsParameterHelper.SecsParam.ProcessProgramPath = recipePath;
                SecsParameterHelper.SecsParemFunc.SaveSecsParament(); ;
            }
        }

        // 刷新文件列表
        private void buttonResfesh_Click(object sender, EventArgs e)
        {
            if (recipePath == null)
            {
                MessageBox.Show("没有选择recipe路径");
            }
            ResfeshRecipeList(recipePath, PPIsFolder);
        }

        // 选中的
        string GetCurSelReciptName()
        {
            if (listViewProcessProgram.SelectedItems.Count > 0)
            {
                return listViewProcessProgram.SelectedItems[0].SubItems[0].Text;
            }
            return null;
        }

        // 刷新recipe目录
        void ResfeshRecipeList(string dir,bool PPIsFolder)
        {
            List<string> listFile;
            
            listFile = pGEMRecipe.GetDirAllFile( dir,  PPIsFolder);

            if (listFile != null)
            {
                listViewProcessProgram.Items.Clear();
                for (int i = 0; i < listFile.Count; i++)
                {
                    listViewProcessProgram.Items.Add(listFile[i]);
                }
            }
        }

        private void buttonUpload_Click(object sender, EventArgs e)
        {
            string ppid = textBox_PPID.Text;
            pGEMRecipe.Upload(ppid);
        }

        private void buttonDownload_Click(object sender, EventArgs e)
        {
            string ppid = textBox_PPID.Text;
            pGEMRecipe.Download(ppid);
        }

        void ShowResult(RcResult rs)
        {
            if (rs != 0)
            {
                MessageBox.Show(rs.toString());
            }
        }

        private void RefreshView()
        {
            textBoxRecipePath.Text = recipePath;
            ResfeshRecipeList(recipePath, PPIsFolder);

            checkBoxPPIsFolar.Checked = PPIsFolder;

            checkBoxAllowDownRecipe.Checked = PPAllowHostDowmload;

        }


        //Recipe是否是文件夹
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            PPIsFolder = checkBoxPPIsFolar.Checked;
            m_pSecs.PPIsFolderDir(PPIsFolder);

            ResfeshRecipeList(recipePath, PPIsFolder);
            // 下面这个是文件夹的前提
            m_pSecs.SetWinrarPath(SecsParameterHelper.SecsParam.WinrarPath);

            SecsParameterHelper.SecsParam.PPIsFolder = PPIsFolder;
            SecsParameterHelper.SecsParemFunc.SaveSecsParament();
        }
        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            PPAllowHostDowmload = checkBoxAllowDownRecipe.Checked;
            m_pSecs.PPAllowHostDowmload(PPAllowHostDowmload);

            SecsParameterHelper.SecsParam.PPAllowHostDowmload = PPAllowHostDowmload;
            SecsParameterHelper.SecsParemFunc.SaveSecsParament();
        }
        

        private void listViewProcessProgram_MouseUp(object sender, MouseEventArgs e)
        {
            string ppidTemp = GetCurSelReciptName();
            textBox_PPID.Text = ppidTemp;
        }

       
    }
}
